package com.opensky.qatest.ui.page;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.ui.component.CategoryPageJoinNag;
import com.opensky.qatest.ui.component.CategotyPageJoinModal;
import com.opensky.qatest.util.common.AssertReport;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.HTML;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Category page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class CategoryPage extends LoadableComponent<CategoryPage> implements IPage {

    // ************************ Locators ************************

    private static final String URL_PATTERN = "https://opensky.com/curators/%s";

    private static final String HELP_LINK = "//a[@href='/content/faq' and text()='Help']";

    private static final String LOGIN_LINK = "//div[@class='login']/a[text()='Log in']";

    private static final String CART_LINK = "//div[@id='cart-nav']//span[@class='cart']";

    private static final String TOP_NAV = "//ul[@id='category-nav']";

    private static final String BOTTOM_NAV = "//div[@class='wrap']/ul[@class='links']";

    private static final String FOLLOW_CURATOR_BTN = "//li[@class='thin'][1]//a[text()='Follow']";

    private static final String TOOL_TIP = "//div[@id='ui-tooltip-0-content' and contains(., 'You must be')]";

    private static final String TOOL_TIP_LINK = "//div[@id='ui-tooltip-0-content']/a";

    private static final String CURATOR_IMG = "//li[@class='thin'][1]//div[@class='curator']//img";

    private static final String CURATOR_IMG_LINK = "//li[@class='thin'][1]//div[@class='curator']//img/..";

    private String categoryPage;

    // ************************ Complex page objects **************************

    private CategotyPageJoinModal joinModal;

    private CategoryPageJoinNag joinNag;

    private final WebDriverWrapper driver;

    public CategoryPage(WebDriverWrapper driver, String category) {
	this.driver = driver;
	joinModal = new CategotyPageJoinModal(driver);
	joinNag = new CategoryPageJoinNag(driver);
	categoryPage = String.format(URL_PATTERN, category.toLowerCase());
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(categoryPage);

    }

    public void clickFollowCuratorBtn() {
	driver.waitAndClick("FOLLOW_CURATOR_BTN", By.xpath(FOLLOW_CURATOR_BTN));
    }

    public CuratorsProfilePage clickCuratorImgAndCheckURL() {
	
	WebElement imgage = driver.waitAndGetElement("CURATOR_IMG", By.xpath(CURATOR_IMG));
	WebElement link = driver.waitAndGetElement("CURATOR_IMG_LINK", By.xpath(CURATOR_IMG_LINK));

	String href = link.getAttribute(HTML.HREF);
	String curator = href.split("/")[3];
	
	CuratorsProfilePage curatorsFrofilePage = new CuratorsProfilePage(driver, curator);

	imgage.click();

	if(curatorsFrofilePage.isPageLoaded()) {
	    return curatorsFrofilePage;
	}
	else {
	    return null;
	}
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() throws Error {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(AssertReport.wrongURL(categoryPage, url), LogicUtils.isURLEqual(categoryPage, url));
    }

    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(categoryPage);
    }

    public boolean isBaseNavElementsPresent() {
	driver.waitAndCheckElementPresent("LOGIN_LINK", By.xpath(LOGIN_LINK));
	driver.waitAndCheckElementPresent("HELP_LINK", By.xpath(HELP_LINK));
	driver.waitAndCheckElementPresent("CART_LINK", By.xpath(CART_LINK));
	driver.waitAndCheckElementPresent("TOP_NAV", By.xpath(TOP_NAV));
	driver.waitAndCheckElementPresent("BOTTOM_NAV", By.xpath(BOTTOM_NAV));
	return true;
    }

    public boolean isToolTipPresent() {
	driver.waitAndCheckElementPresent("TOOL_TIP", By.xpath(TOOL_TIP));
	return true;
    }

    public LoginPage clickToolTipLogIn() {
	
	LoginPage loginPage = new LoginPage(driver);
	
	driver.waitAndClick("TOOL_TIP_LINK", By.xpath(TOOL_TIP_LINK));
	
	if(loginPage.isPageLoaded()) {
	    return loginPage;
	}
	else {
	    return null;
	}
    }

    // ************************ GET/SET methods **************************

    public CategotyPageJoinModal getJoinModal() {
	return joinModal;
    }

    public CategoryPageJoinNag getJoinNag() {
	return joinNag;
    }
}
